package com.umeng.soexample.mvp.view.fragment;


import android.database.Cursor;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.umeng.soexample.R;
import com.umeng.soexample.bean.UserEntity;
import com.umeng.soexample.dao.USerService;
import com.umeng.soexample.mvp.view.custom.QrCodeUtil;
import com.umeng.soexample.network.API;

/**
 * 本地二维码显示
 * A simple {@link Fragment} subclass.
 */
public class ZXingFragment extends Fragment implements View.OnClickListener {

    private View view;
    private ImageView iv_ZXing_back;
    private ImageView iv_ZXing_user_head;
    private TextView tv_ZXing_user_name;
    private TextView tv_ZXing_nick_name;
    private ImageView iv_ZXing_qr_code;
    private TextView iv_ZXing_push;

    private UserEntity.ValuesBean user;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_zxing, container, false);

        initView(view);
        initData();
        initListener();
        return view;
    }

    /**
     * 初始化数据
     */
    private void initData() {
        Cursor cursor = USerService.getInstance().query("user", null, null);
        if (cursor.moveToNext()){
            user = new UserEntity.ValuesBean();
            user.user_id = cursor.getString(cursor.getColumnIndex("user_id"));
            user.nick_name = cursor.getString(cursor.getColumnIndex("nick_name"));
            user.user_head = cursor.getString(cursor.getColumnIndex("user_head"));
            user.nick_name = cursor.getString(cursor.getColumnIndex("nick_name"));
            user.qr_code = cursor.getString(cursor.getColumnIndex("qr_code"));

        }

        if (user != null){
            Glide.with(getContext()).load(API.BaseUrl + user.user_head).into(iv_ZXing_user_head);
            tv_ZXing_user_name.setText(user.user_name);
            tv_ZXing_nick_name.setText(user.nick_name);
            if (TextUtils.isEmpty(user.qr_code)){
                Bitmap bitmap = QrCodeUtil.createQRCode(user.toString());
                iv_ZXing_qr_code.setImageBitmap(bitmap);
            }else{
                Glide.with(getContext()).load(API.BaseUrl + user.qr_code).into(iv_ZXing_qr_code);
            }
        }

    }

    /**
     * 初始化监听
     */
    private void initListener() {
        iv_ZXing_back.setOnClickListener(this);
        iv_ZXing_push.setOnClickListener(this);
    }

    /**
     * 初始化视图
     * @param view
     */
    private void initView(View view) {
        iv_ZXing_back = (ImageView) view.findViewById(R.id.iv_ZXing_back);
        iv_ZXing_user_head = (ImageView) view.findViewById(R.id.iv_ZXing_user_head);
        tv_ZXing_user_name = (TextView) view.findViewById(R.id.tv_ZXing_user_name);
        tv_ZXing_nick_name = (TextView) view.findViewById(R.id.tv_ZXing_nick_name);
        iv_ZXing_qr_code = (ImageView) view.findViewById(R.id.iv_ZXing_qr_code);
        iv_ZXing_push = (TextView) view.findViewById(R.id.iv_ZXing_push);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_ZXing_back:
                getActivity().finish();
                break;
            case R.id.iv_ZXing_push:
                Toast.makeText(getContext(), "刷新", Toast.LENGTH_SHORT).show();
                break;

        }
    }
}
